import 'dart:convert';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:initproject/bloc/userbloc/UserEntity.dart';
import 'package:mybaseplus/base/tool/empty/FFEmpty.dart';
import 'package:mybaseplus/base/tool/sp/FFSpUtils.dart';

part 'user_event.dart';
part 'user_state.dart';


/**
 * 取
 * var userEntity = BlocProvider.of<UserBloc>(context).state.userEntity;
 *
 * 更新
 * BlocProvider.of<UserBloc>(context).add(UpdateUserEvent(userState: UserState(userEntity: p0.data)));
 *
 *
 * builder
 *
    BlocBuilder<UserBloc,UserState>(
    builder: (context, state) {
    return Container();
    },
    )
 */

class UserBloc extends Bloc<UserEvent, UserState> {
  static String userInfo = "UserBloc.userInfo";

  UserBloc() : super(UserState(userEntity: UserBlocTool.getUser())) {
    on<UserEvent>((event, emit) async {
      if (event is UpdateUserEvent) {
        ///更新
        assert(event.userState.userEntity != null, "UserBloc UpdateUserEvent更新事件，UserEntity一定要有值");
        UserEntity? userEntity = event.userState.userEntity;
        UserBlocTool.userEntity = userEntity;
        FFSpUtils.saveData(UserBloc.userInfo, jsonEncode(userEntity?.toJson()));
        emit(UserState(userEntity: userEntity));
      } else if (event is UnLoginUserEvent) {
        await FFSpUtils.remove(UserBloc.userInfo);
        emit(UserState(userEntity: null));
      }
    });
  }
}

/**
 * UserBloc操作集中管理
 */
class UserBlocTool {
  ///缓存当前用户信息
  static UserEntity? userEntity;

  ///更新 userEntity
  static update(BuildContext context, UserEntity? uEntity) {
    userEntity = uEntity;
    BlocProvider.of<UserBloc>(context).add(UpdateUserEvent(userState: UserState(userEntity: uEntity)));
  }

  ///退出登录操作
  static loginOut(BuildContext context) async {
    userEntity = null;
    BlocProvider.of<UserBloc>(context).add(UnLoginUserEvent(userState: UserState(userEntity: null)));
  }

  ///返回当前用户对象
  static UserEntity? getUser() {
    if (userEntity != null) {
      return userEntity;
    }

    try {
      String? json = FFSpUtils.getData(UserBloc.userInfo);
      if (!FFEmpty.stringIsEmpty(json)) {
        UserEntity user = UserEntity.fromJson(jsonDecode(json!));
        userEntity = user;
        return user;
      }
      return null;
    } catch (e) {
      return null;
    }
  }

  ///是否登录
  static bool isLogin() {
    UserEntity? entity = getUser();
    if (entity != null) {
      return !FFEmpty.stringIsEmpty(entity.id);
    }
    return false;
  }

  ///返回当前用户对象uid
  static String uid() {
    UserEntity? userEntity = getUser();

    if (userEntity != null) {
      if (!FFEmpty.stringIsEmpty(userEntity.id)) {
        return userEntity.id!;
      }
    }

    return "";
  }

  ///返回当前用户对象token
  static String token() {
    UserEntity? userEntity = getUser();

    if (userEntity != null) {
      if (!FFEmpty.stringIsEmpty(userEntity.token)) {
        return userEntity.token!;
      }
    }

    return "";
  }

  ///是否是订阅用户
  static bool isPremium() {
    return false;
  }

  /// 快速使用
  static widget(Function(BuildContext ctx, UserState obj) builder) {
    return BlocBuilder<UserBloc, UserState>(
      builder: (context, state) {
        return builder(context, state);
      },
    );
  }
}
